package com.zztl.infiniteBand.ui.activity

import android.content.Intent
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.andview.refreshview.XRefreshView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.RecordAdapter
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.eventbus.RefreshAssetsEvent
import com.zztl.infiniteBand.model.TradingRecordBean
import com.zztl.infiniteBand.net.requestBean.TradingRecordRequest
import com.zztl.infiniteBand.presenter.RecordPresennter
import com.zztl.infiniteBand.ui.view.mvpview.RecordView
import com.zztl.infiniteBand.ui.view.recycleview.GroupInfo
import com.zztl.infiniteBand.ui.view.recycleview.HeaderDecoration
import com.zztl.infiniteBand.ui.view.recycleview.SimpleItemDecoration
import com.zztl.infiniteBand.util.UiUtils
import kotlinx.android.synthetic.main.activity_record.*
import org.greenrobot.eventbus.EventBus

class RecordActivity : BaseMvpActivity<RecordView, RecordPresennter>(), RecordView, BaseQuickAdapter.OnItemClickListener {

    /* @BindView(R.id.rv)
     lateinit var rv: RecyclerView
     @BindView(R.id.xrefreshview)
     lateinit var xrefreshview: XRefreshView
     @BindView(R.id.ll_nodata)
     lateinit var ll_nodata: LinearLayout*/


    private var mAdapter: RecordAdapter? = null


    private var mHandler: Handler? = Handler()
    private var mRecordRequest: TradingRecordRequest? = null
    private var mLayoutManager: LinearLayoutManager? = null
    lateinit var datas: List<TradingRecordBean.DataBean.RowsBean>
    lateinit var mPresennter: RecordPresennter
    private var page = 1
    private val limit = 10

    override fun setLayoutId(): Int = R.layout.activity_record


    override fun initBase() {
        setTitle(getString(R.string.收益记录))
        datas = arrayListOf()
        initRecycleview()
        mRecordRequest = TradingRecordRequest(page, limit)
        mPresennter.getRecordData(mRecordRequest!!, this)

    }

    private fun initRecycleview() {
        mAdapter = RecordAdapter(R.layout.item_record, datas)
        mAdapter!!.onItemClickListener = this@RecordActivity
        mLayoutManager = LinearLayoutManager(this)
        rv.layoutManager = mLayoutManager
        rv.adapter = mAdapter
        rv.addItemDecoration(SimpleItemDecoration(this))
        /*       val callback = HeaderDecoration.GroupInfoCallback {
                   postition ->  }*/
        val callback = object : HeaderDecoration.GroupInfoCallback {
            override fun getGroupInfo(postition: Int): GroupInfo? {
                return this@RecordActivity.getGroupInfo(postition)
            }
        }
        rv.addItemDecoration(HeaderDecoration(callback))

        xrefreshview.let { it ->
            it.setPinnedTime(800)
            it.pullLoadEnable = true
            it.setAutoLoadMore(false)
            it.setXRefreshViewListener(object : XRefreshView.SimpleXRefreshListener() {
                override fun onRefresh(isPullDown: Boolean) {

                    page = 1
                    mRecordRequest!!.page = page
                    mPresennter.getRecordData(mRecordRequest!!, this@RecordActivity)
                }

                override fun onLoadMore(isSilence: Boolean) {
                    mRecordRequest!!.page = ++page
                    mPresennter.getRecordData(mRecordRequest!!, this@RecordActivity)
                }
            })
        }

    }


    private fun getGroupInfo(position: Int): GroupInfo {
        var length = 0
        var firstPosition = 0
        val data = mAdapter!!.data
        val yearMonth = data[position].yearMonth
        val split = yearMonth!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        val groupInfo = GroupInfo(split[0] + UiUtils.getString(R.string.年) + split[1] + UiUtils.getString(R.string.月))
        for (i in data.indices) {
            if (data[i].yearMonth == yearMonth) {
                length++
            }
            if (1 == length) {
                firstPosition = i
            }
        }

        groupInfo.setGroupLength(length)
        groupInfo.setPosition((position - firstPosition) % length)
        return groupInfo
    }

    override fun createPresenter(): RecordPresennter {
        mPresennter = RecordPresennter()
        return mPresennter
    }


    override fun getRecordDataSuccess(tradingRecordBean: TradingRecordBean) {
        datas = tradingRecordBean.data!!.rows!!
        if (datas.size == 0 && mRecordRequest!!.page == 1) {
            //            mTvNodata.setVisibility(View.VISIBLE);
            //            xrefreshview.setVisibility(View.GONE);
            rv.visibility = View.GONE
            ll_nodata.visibility = View.VISIBLE
            xrefreshview.pullLoadEnable = false
        } else {

            rv.visibility = View.VISIBLE
            ll_nodata.visibility = View.GONE
            xrefreshview.pullLoadEnable = true
            if (mRecordRequest!!.page == 1) {
                mAdapter!!.setNewData(datas)
            } else {
                mAdapter!!.addData(datas)
            }
        }
    }


    override fun getDeviceSuccessOrFail() {
        xrefreshview.stopRefresh()
        xrefreshview.stopLoadMore(true)
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val recordId = mAdapter!!.data[position].id
        //        TextView tvStatus = (TextView) view.findViewById(R.id.tv_time);
        val intent = Intent(this, RecordDetailActivity::class.java)
        intent.putExtra("recordId", recordId)
        /*    if (tvStatus.getVisibility() == View.VISIBLE) {
            intent.putExtra("status", tvStatus.getText().toString());
        }*/
        showActivity(intent)
    }


    override fun onDestroy() {
        EventBus.getDefault().post(RefreshAssetsEvent())
        mHandler?.apply {
            removeCallbacksAndMessages(null)
            null
        }

        super.onDestroy()
    }


}
